package com.fs.ibox.dao.impl;

import com.fs.ibox.dao.CollectionDao;
import com.fs.ibox.pojo.Collection;
import com.fs.ibox.util.JDBCUtil;
import com.fs.ibox.vo.Search;

import java.util.List;

/**
 * Author:HOU
 * date:2022/11/15  15:31
 * 案例:
 **/
public class CollectionDaoimpl implements CollectionDao {


    @Override
    public List<Collection> selectAll() {
        return null;
    }

    /**
     * 查询藏品的图片路径
     * @param cname 藏品名称
     * @return
     */
    @Override
    public String queryImg(String cname) {
        String sql = "SELECT cimg FROM tb_collection WHERE cname=?";
        List<String> srcs = JDBCUtil.executeQuery(sql, String.class, cname);
        return  srcs == null || srcs.isEmpty()?null:srcs.get(0);
    }

    @Override
    public List<Collection> queryAll() {
        String sql="SELECT * FROM tb_collection ORDER BY startdate desc;";
        List<Collection> tbCollections = JDBCUtil.executeQuery(sql, Collection.class);
        return tbCollections;
    }

    @Override
    public void addCollection(Collection collection) {
        String sql="INSERT INTO tb_collection (cname,type,releasenum,price,content,startdate,enddate,cimg,residual) VALUES (?,?,?,?,?,?,?,?,?)";
        JDBCUtil.executeUpdate(sql,collection.getCname(),collection.getType(),collection.getReleasenum(),collection.getPrice(),collection.getContent(),collection.getStartdate(),collection.getEnddate(),collection.getCimg(),collection.getResidual());
    }

    @Override
    public List<Collection> queryById(int id) {
        String sql="SELECT * FROM tb_collection where id=?";
        List<Collection> collections = JDBCUtil.executeQuery(sql, Collection.class,id);
        System.out.println(collections);
        return collections;
    }

    @Override
    public Collection queryByName(String name) {
        String sql = "SELECT * FROM tb_collection where cname = ?";
        List<Collection> collections = JDBCUtil.executeQuery(sql, Collection.class, name);
        return collections.get(0);
    }

    @Override
    public void reviseByID(int id) {
        String sql="UPDATE tb_collection set residual=residual-1 WHERE id=?";
        int i = JDBCUtil.executeUpdate(sql, id);
    }

    @Override
    public List<Search> queryUserIdcoll(String keywords, String state,String username) {
        String sql="";
        if("1".equals(state)){
             sql="SELECT a.cimg ,a.releasenum ,b.cname,b.cid,b.price FROM tb_collection a JOIN tb_idcollection b ON a.cname=b.cname WHERE b.cstate=1  AND username!=? and b.cname LIKE ? ORDER BY price ASC";
        }
        else {
            sql="SELECT a.cimg ,a.releasenum ,b.cname,b.cid,b.price FROM tb_collection a JOIN tb_idcollection b ON a.cname=b.cname WHERE b.cstate=1  AND username!=? and b.cname LIKE ? ORDER BY price DESC";
        }
        List<Search> searches = JDBCUtil.executeQuery(sql, Search.class,username,keywords);


        return searches;
    }


}
